import MyUpload from '@/components/MyUpload';
import { editAdvertising } from '@/services/advertising';
import { getResult } from '@/services/sys/attribute';
import { findByDataCategoryListPageSelect } from '@/services/sys/MenuService';
import {
  ModalForm,
  ProForm,
  ProFormGroup,
  ProFormItem,
  ProFormSelect,
  ProFormText,
} from '@ant-design/pro-components';
import { Button, message } from 'antd';
import React, { useEffect } from 'react';
interface Props {
  row: any;
  success?: () => void;
}

/*  const columnDialogs = [{
    title: "编号",
    key: "id",
    dataIndex: "id",
    search: false,
    formItemProps: {
      hidden: true
    }
  }, {
    title: "编号",
    key: "imageId",
    dataIndex: "imageId",
    search: false,
    formItemProps: {
      hidden: true
    }
  }, {
    title: "基本信息", valueType: "group", columns: [{
      title: "图片",
      key: "image",
      dataIndex: "image",
      valueType: "image",
      fieldProps: {
        width: 200,
        height: 100
      },
      width: 'md', renderFormItem: (item: any, f: any, form: any) => {
        const image = form.getFieldValue ( 'image' );
        const id = form.getFieldValue ( 'id' );
        return (<> <MyUpload
            type={4}
            url={image}
            id={id}
            onChange={(event) => {
console.log("MyUpload",event)
              formRef.current?.setFieldsValue ( {imageId: event} );
            }}
          ></MyUpload></>
        )

      }
    }, {
      title: "标题",
      key: "title",
      dataIndex: "title",
      width: 'md'
    }, {
      title: "广告类型",
      key: "type",
      dataIndex: "type",
      valueType: "select",
      request: async () => {
        return await getResult ( 'advertisingManagementType', 1 );
      },
      width: 'md'
    }, {
      title: "目标类型",
      key: "target_type",
      dataIndex: "type",
      valueType: "select",

      request: async () => {
        return await getResult ( 'advertisingTargetType', 1 );
      },
      width: 'md'
    }, {
      title: '地址',
      dataIndex: 'path',
      key: 'path',
      width: 'sm',
      valueType: "select",
      request: request1
    }, {
      title: "APPID",
      key: "appid",
      dataIndex: "appid",
      width: 'md'
    }, {
      title: "备注",
      key: "remark",
      dataIndex: "remark",
      valueType: "textarea",
      width: 'md'
    }]
  }]*/
const requestType = async () => {
  return new Promise<API.SelectItem[]>((resolve, reject) => {
    getResult('advertisingManagementType', 1)
      .then((res) => {
        let selectItems = res;
        // @ts-ignore
        //   selectItems.unshift({ name: '下一单', label: '下一单', value: "" });
        resolve(selectItems);
      })
      .catch((e) => {
        console.log(e);
        reject(e);
      });
  });
};
const request1 = async () => {
  return new Promise<API.SelectItem[]>((resolve, reject) => {
    findByDataCategoryListPageSelect()
      .then((res) => {
        let selectItems = res;
        // @ts-ignore
        selectItems.unshift({ name: '下一单', label: '下一单', value: '' });
        resolve(selectItems);
      })
      .catch((e) => {
        console.log(e);
        reject(e);
      });
  });
};
const AdvEdit: React.FC<Props> = (props) => {
  const { row, success } = props;

  const [form] = ProForm.useForm();
  useEffect(() => {
    form.setFieldsValue(row);
  }, [row]);
  // @ts-ignore
  return (
    <ModalForm<API.MenuInfo>
      initialValues={row}
      title="菜单管理"
      form={form}
      trigger={<Button type="primary">新增/编辑</Button>}
      onFinish={(values) => {
        // values.id = menuInfo.id;
        console.log(values);
        return new Promise<Boolean>((resolve, reject) => {
          editAdvertising(values)
            .then(() => {
              message.success('操作成功');

              //     refresh ();
              resolve(true);
              if (success) {
                success();
              }
              form.resetFields();
            })
            .catch(() => {
              message.success('操作失败');
              reject(false);
            });
        });
      }}
    >
      <ProFormText hidden={true} width="md" name="id" />
      <ProFormText hidden={true} width="md" name="imageId" />
      <ProFormGroup>
        <ProFormItem label={'图标'} name={'image'}>
          <MyUpload
            type={4}
            onChange={(imageId) => {
              form.setFieldValue('imageId', imageId);
            }}
          />
        </ProFormItem>
        <ProFormText width={'md'} label={'名称'} name={'title'}></ProFormText>
      </ProFormGroup>
      <ProFormGroup>
        <ProFormSelect
          request={requestType}
          width={'md'}
          label={'广告类型'}
          name={'type'}
        ></ProFormSelect>
      </ProFormGroup>
      <ProFormGroup>
        <ProFormText width={'md'} label={'备注'} name={'remark'}></ProFormText>
        <ProFormText width={'md'} label={'APPID'} name={'appid'}></ProFormText>
      </ProFormGroup>
      <ProFormGroup>
        <ProFormSelect
          request={() => {
            return getResult('advertisingTargetType', 1);
          }}
          width={'md'}
          label={'目标类型'}
          name={'targetType'}
        ></ProFormSelect>
        <ProFormSelect
          request={request1}
          width={'md'}
          label={'地址'}
          name={'path'}
        ></ProFormSelect>
      </ProFormGroup>
    </ModalForm>
  );
};
export default AdvEdit;
